package camera;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

@SuppressLint({ "NewApi", "ViewConstructor" })
public class CameraSurface extends SurfaceView implements Callback {

	private Camera camera;
	private SurfaceHolder holder;

	@SuppressWarnings("deprecation")
	public CameraSurface(Context context, Camera cam) {
		super(context);
		camera = cam;
		holder = getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			camera.setPreviewDisplay(holder);
			camera.startPreview();
		} catch (Exception e) {

		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	    if (camera!=null)
	    {
	        camera.stopPreview();
	        camera.release();
	        camera=null;
	    }


	}

}
